<?php


namespace App\backend\models;


use App\common\models\BaseModel;

class MemberLevelModel extends BaseModel
{

    // 表名
    const TABLE_NAME = "member_level";
    public $table = self::TABLE_NAME;


    /**
     * 关联 member 数据表 一对多
     */
    public function member()
    {
        return $this->hasMany(MemberModel::class, 'level_id', 'id');
    }

    /**
     * 获取会员分组列表
     */
    public static function getLevelPageList($params)
    {
        $keyword = isset($params['keyword'])?trim($params['keyword']):'';
        $where = [];
        if ($keyword){
            $where[] = ['level_name','like','%'.$keyword.'%'];
        }
        $page = !isset($params['page']) ? 1 : $params['page'];unset($params['page']);
        $limit = !isset($params['limit']) ? 10 : $params['limit'];unset($params['limit']);

        $result = self::query()->select(['id', 'level_name'])
            ->with(['member' => function($query){
                return $query->select(['uid','level_id']);
            }])->where($where)->paginate($limit, ['*'], 'page', $page)->toArray();

        return ['data'=>$result['data'],'total'=>$result['total']];

    }

}
